<template>
  <div>
    <BasicTable @register="registerTable" :can-resize="true" :bordered="true" :striped="false">
      <template #toolbar>
        <a-button type="primary" @click="onAddDutyGroup"> 新增 </a-button>
      </template>

      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              icon: 'clarity:note-edit-line',
              label: '编辑',
              onClick: onEditDutyGroup.bind(null, record),
            },
            {
              icon: 'ant-design:delete-outlined',
              label: '删除',
              color: 'error',
              popConfirm: {
                title: '是否确认删除',
                confirm: onDeleteDutyGroup.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>

    <addOrUpdate @register="registerAddOrUpdateForm" @success="afterSuccess" />
  </div>
</template>
<script lang="ts">
  import { defineComponent, ref, provide } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';

  import { OperationTypeEnum } from '/@/enums/appEnum';
  import { useMessage } from '/@/hooks/web/useMessage';

  import { getAllDutyGroup, deleteDutyGroup } from '/@/api/duty/dutygroup';
  import { dutyGroupTableColumns } from './dutygroup.data';

  //引入子组件
  import addOrUpdate from './AddOrUpdate.vue';

  //使用modal弹出窗口
  import { useModal } from '/@/components/Modal';
  const { createMessage } = useMessage();

  const [registerAddOrUpdateForm, { openModal: openAddOrUpdateForm }] = useModal();

  export default defineComponent({
    name: 'DutyGroupManagement',
    components: { BasicTable, TableAction, addOrUpdate },
    setup() {
      //定义模块的名称，提供给子孙组件获取使用
      const moduleName = ref('值班分组管理');
      provide('moduleName', moduleName);

      /**
       * 请求后台查询接口返回数据显示
       */
      const [registerTable, { reload }] = useTable({
        title: '值班分组管理',
        api: getAllDutyGroup,
        columns: dutyGroupTableColumns, //表头数据
        showTableSetting: true, //是否显示表格设置
        showIndexColumn: true,
        actionColumn: {
          width: 100,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: 'right', //表格操作列是否固定，不设置时默认固定在右侧
        },
        pagination: false, //关闭分页
      });

      /**
       * 打开值班分组新增窗口
       */
      function onAddDutyGroup() {
        //使用props传递数据至子组件，在data参数中指定
        openAddOrUpdateForm(true, { op: OperationTypeEnum.ADD });
      }

      /**
       * 打开值班分组编辑窗口
       */
      function onEditDutyGroup(record: Recordable) {
        openAddOrUpdateForm(true, { op: OperationTypeEnum.EDIT, dataId: record.id });
      }

      /**
       * 删除值班分组
       */
      function onDeleteDutyGroup(record: Recordable) {
        deleteDutyGroup(record.id)
          .then((res) => {
            console.log('res', res);
            //res判断结果：同时==undefined和null
            if (res == undefined && (res == null || Object.keys(res).length == 0)) {
              createMessage.success('删除成功。', 2);
              afterSuccess();
            }
          })
          .catch((ex) => {
            console.log('catch ', ex);
            createMessage.error(ex.msg);
          });
      }

      /**
       * 弹窗确认操作之后刷新页面
       */
      function afterSuccess() {
        reload();
      }

      return {
        moduleName,
        registerTable,
        afterSuccess,
        registerAddOrUpdateForm,
        onAddDutyGroup,
        onEditDutyGroup,
        onDeleteDutyGroup,
      };
    },
  });
</script>
